#!/bin/sh

e2e_test_install() {
  CLUSTER_NAME="$(get_sgcluster_name service-conf-dl)"
  NODE_LABEL_KEY="$(random_string)"
  NODE_LABEL_VALUE="$(random_string)"

  kubectl create namespace "$CLUSTER_NAMESPACE"

  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 2 \
    --set "cluster.pods.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE" \
    --set-string cluster.distributedLogs.sgDistributedLogs=distributedlogs \
    --set distributedLogs.enabled=true \
    --set-string "distributedLogs.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE"
}

e2e_test() {
  run_test "Creating a cluster with distributed logs services enabled using default parameters" distributedlogs_services_enabled_by_default

  run_test "Creating a cluster with distributed logs services disabled" distributedlogs_services_full_enabled

  run_test "Creating a cluster with services enabled with specific annotations" services_annotations
}

distributedlogs_services_enabled_by_default(){
  if [ "$(kubectl get sgdistributedlogs -n "$CLUSTER_NAMESPACE" \
    --template "{{ (index .items 0).spec.postgresServices.primary.type }}")" \
    = "ClusterIP" ]
  then
    success "Distributed logs primary postgres service was created correctly and with type ClusterIP by default"
  else
    fail "Distributed logs primary postgres service was not created correctly and with type ClusterIP by default"
  fi

  if [ "$(kubectl get sgdistributedlogs -n "$CLUSTER_NAMESPACE" \
    --template "{{ (index .items 0).spec.postgresServices.replicas.enabled }}")" \
    = "true" ]
  then
    success "Distributed logs replicas are enabled"
  else
    fail "Distributed logs replicas are not enabled"
  fi

  if [ "$(kubectl get sgdistributedlogs -n "$CLUSTER_NAMESPACE" \
    --template "{{ (index .items 0).spec.postgresServices.replicas.type }}")" \
    = "ClusterIP" ]
  then
    success "Distributed logs posgtres service replicas was created correctly and with type ClusterIP by default"
  else
    fail "Distributed logs posgtres service replicas was not created correctly and with type ClusterIP by default"
  fi

  remove_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"
}

distributedlogs_services_full_enabled(){
  NODE_LABEL_KEY="$(random_string)"
  NODE_LABEL_VALUE="$(random_string)"

  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 2 \
    --set "cluster.pods.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE" \
    --set-string cluster.distributedLogs.sgDistributedLogs=distributedlogs \
    --set distributedLogs.enabled=true \
    --set distributedLogs.postgresServices.primary.type="NodePort" \
    --set distributedLogs.postgresServices.replicas.enabled="true" \
    --set distributedLogs.postgresServices.replicas.type="NodePort" \
    --set-string "distributedLogs.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE"

  if [ "$(kubectl get sgdistributedlogs -n "$CLUSTER_NAMESPACE" \
    --template "{{ (index .items 0).spec.postgresServices.primary.type }}")" \
    = "NodePort" ]
  then
    success "Distributed logs primary service was created correctly and with type NodePort"
  else
    fail "Distributed logs primary service was not created with correct type NodePort"
  fi

  if [ "$(kubectl get sgdistributedlogs -n "$CLUSTER_NAMESPACE" \
    --template "{{ (index .items 0).spec.postgresServices.replicas.enabled }}")" \
    = "true" ]
  then
    success "Distributed logs replicas are enabled"
  else
    fail "Distributed logs replicas are not enabled"
  fi

  if [ "$(kubectl get sgdistributedlogs -n "$CLUSTER_NAMESPACE" \
    --template "{{ (index .items 0).spec.postgresServices.replicas.type }}")" \
    = "NodePort" ]
  then
    success "Distributed logs replica has the type NodePort"
  else
    fail "Distributed logs replica has not the type NodePort"
  fi

  remove_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"
}

services_annotations(){
  local PRIMARY_RANDOM_VALUE="$(random_string)"
  local REPLICA_RANDOM_VALUE="$(random_string)"
  
  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 2 \
    --set "cluster.pods.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE" \
    --set-string cluster.distributedLogs.sgDistributedLogs=distributedlogs \
    --set distributedLogs.enabled=true \
    --set distributedLogs.postgresServices.replicas.enabled="true" \
    --set-string "distributedLogs.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE" \
    --set cluster.metadata.annotations.primaryService.primary-annotation="$PRIMARY_RANDOM_VALUE" \
    --set cluster.metadata.annotations.replicasService.replica-annotation="$REPLICA_RANDOM_VALUE"

  SERVICE_ANNOTATION="primary-annotation"
  wait_until check_service_annotation "" "$PRIMARY_RANDOM_VALUE"

  SERVICE_ANNOTATION="replica-annotation"
  wait_until check_service_annotation "replicas" "$REPLICA_RANDOM_VALUE"

  remove_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"
}
